window.onload=function () {
    getotaldata();
}
function postshops() {//提交订单
    var num=document.getElementById("shops_allnum").innerText;
    var price=document.getElementById("shop_allprice").innerText;
    if(num==0||price==0||num==''||price==''){
        alert("你购物车空的，你提交个啥??你个******");
    }else{
        con=confirm("确认提交?");
        if(con==true) {
            $.ajax({
                url: "postshops.do",
                type: "POST",
            });
            alert("提交成功，我们会尽快处理!");
            document.getElementById("shops_allnum").innerText = 0;
            document.getElementById("shop_allprice").innerText = 0;
            window.location.href="food.jsp";
            // var tab = document.getElementById("data_table");//表格删除
            // var len = tab.rows.length;
            // if(len==1){return;}
            // else{
            //     for (var i = 1; i < len-1; i++)
            //         tab.deleteRow(i);
            // }
            // tab.deleteRow(1);
        }
    }
}
function getotaldata() {
    var tab=document.getElementById("data_table");
    var len=tab.rows.length;
    var shop_num=0;
    var shop_price=0;
    var num_sun=0;
    var price_sum=0;
    for(var i=1;i<len-1;i++){
        shop_num=Number(tab.rows[i].cells[2].getElementsByTagName("input")[0].value);//数量
        shop_price=Number((tab.rows[i].cells[3].innerText).substring(1));//单价
        num_sun=num_sun+shop_num;
        price_sum=price_sum+(shop_price*shop_num);
    }
    document.getElementById("shops_allnum").innerText=num_sun;
    document.getElementById("shop_allprice").innerText=price_sum;
}
function add(shop_id) {
    var shop_num=document.getElementById('shop_num_'+shop_id).value;
    var shop_price=document.getElementById('shop_price_'+shop_id).innerText;
    var shop_total=document.getElementById('shop_total_'+shop_id).innerText;
    if(shop_num==99){
        alert("不能再加啦！");
    }
    else{
        shop_num++;
        document.getElementById('shop_num_'+shop_id).value=shop_num;
        var str_shop_price=(Number)(shop_price.substring(1));
        var str_shop_total=(Number)(shop_total.substring(1));
        str_shop_total=(shop_num*str_shop_price).toFixed(1);
        document.getElementById('shop_total_'+shop_id).innerText="￥"+str_shop_total;
        $.ajax({
            url:"num_change.do",
            type:"POST",
            data:{shop_id:shop_id,show_num:shop_num,shop_price:str_shop_price},
        });
        getotaldata();
    }
}
function jian(shop_id) {
    var shop_num=document.getElementById('shop_num_'+shop_id).value;
    var shop_price=document.getElementById('shop_price_'+shop_id).innerText;
    var shop_total=document.getElementById('shop_total_'+shop_id).innerText;
    if(shop_num==1){
        alert("不能再减啦！");
    }
    else{
        shop_num--;
        document.getElementById('shop_num_'+shop_id).value=shop_num;
        var str_shop_price=(Number)(shop_price.substring(1));
        var str_shop_total=(Number)(shop_total.substring(1));
        str_shop_total=(shop_num*str_shop_price).toFixed(1);
        document.getElementById('shop_total_'+shop_id).innerText="￥"+str_shop_total;
        $.ajax({
            url:"num_change.do",
            type:"POST",
            data:{shop_id:shop_id,show_num:shop_num,shop_price:str_shop_price},
        });
        getotaldata();
    }
}
function delt(shop_id,obj) {
    var con=confirm("你要删除该商品吗?");
    if(con==true){
        $.ajax({
            url:"delete_shop.do",
            type:"POST",
            data:{shop_id:shop_id},
        });
        var tr=obj.parentNode.parentNode;//删除表格里的该行
        var tab=tr.parentNode;
        tab.removeChild(tr);
        getotaldata();
    }
}
layui.use('table', function(){
    var table = layui.table;
    table.init('parse-table-demo', { //转化静态表格
        //height: 'full-500'
    });
});